const RequestLocal = {
    postLocal(jsonData) {
        let url = RequestLocal.getUrl();
        let postData = RequestLocal.modifyRequestBody(jsonData.requestBody);
        console.log('postData:', postData);
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'POST',
                url: url,
                data: postData,
                headers: Config.local_headers,
                onload: function (response) {
                    const data = response.responseText;
                    const curl = Utils.generateCurl('POST', url, postData, Config.local_headers)
                    resolve({data, curl});
                },
                onerror: function (error) {
                    reject(new Error('请求失败: ' + error.statusText));
                },
                ontimeout: function () {
                    reject(new Error('请求超时'));
                }
            });
        });
    },
    getResult(bizId, requestMethod, businessGuid) {
        const params = new URLSearchParams({
            bizId: bizId,
            requestMethod: requestMethod,
            businessGuid: businessGuid
        });

        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'POST',
                url: `http://localhost:8082/pub/40040901/mum/v1/message/inquiry/query-log-by-bizid?${params}`,
                headers: Config.local_headers,
                onload: function (response) {
                    const result = JSON.parse(response.responseText);
                    resolve(result);
                },
                onerror: function (error) {
                    reject(new Error('请求失败: ' + error.statusText));
                },
                ontimeout: function () {
                    reject(new Error('请求超时'));
                }
            });
        });
    },
    getUrl() {
        const urlParams = new URLSearchParams(window.location.search);
        const type = urlParams.get('type');
        return "http://localhost:8082/pub/40040901/message/channel-send";
    },
    modifyRequestBody(requestBody) {
        let body_dict = JSON.parse(JSON.stringify(requestBody));
        body_dict.tenantCode = 'jmtest';

        if ('requestId' in body_dict) {
            body_dict.requestId = "";
        }

        if (Array.isArray(body_dict.receivers) && body_dict.receivers.length > 0) {
            body_dict.receivers[0] = {
                ...body_dict.receivers[0],
                receiverId: "4230bc6e-69e6-46a9-a39e-b929a06a84e8",
                receiverCode: "admin"
            };
        }
        return JSON.stringify(body_dict);
    },
    postLocal2(url, headers, data) {
        let curl = Utils.generateCurl('POST', url, JSON.stringify(data), headers);
        console.log('CURL postLocal2:');
        console.log(curl);
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'POST',
                url: url,
                data: JSON.stringify(data),
                headers: headers,
                onload: function (response) {
                    const jsonData = JSON.parse(response.responseText);
                    resolve(jsonData);
                },
                onerror: function (error) {
                    reject(new Error('请求失败: ' + error.statusText));
                },
                ontimeout: function () {
                    reject(new Error('请求超时'));
                }
            });
        });
    },
}